package com.atguigu.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastConnectedStream;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

public class Flink03_Operator_BCState {
    public static void main(String[] args) {

        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);
        env.enableCheckpointing(3000);

        //创建两个输出流对象
        DataStreamSource<String> normalData = env.socketTextStream("hadoop162", 9999);
        DataStreamSource<String> controlData = env.socketTextStream("hadoop162", 8888);
        MapStateDescriptor<String, String> desc = new MapStateDescriptor<>("bcState", String.class, String.class);

        //首先将广播数据做成广播流
        BroadcastStream<String> bcStream = controlData.broadcast(desc);

        //对两个流进行合并处理
        BroadcastConnectedStream<String, String> connectStream = normalData.connect(bcStream);

        //对数据进行处理
        connectStream.process(new BroadcastProcessFunction<String, String, String>() {
            @Override
            public void processElement(String value,
                                       ReadOnlyContext ctx,
                                       Collector<String> out) throws Exception {
                //获取到广播状态中的值
                ReadOnlyBroadcastState<String, String> bcData = ctx.getBroadcastState(desc);
                String str = bcData.get("switch");
                //根据广播状态中的值对normalData中的数据进行逻辑处理
                if ("1".equals(str)) {
                    out.collect("这是1号逻辑");
                }else if ("2".equals(str)){
                    out.collect("这是2号·逻辑！");
                }else{
                    out.collect("这是默认逻辑！");
                }
            }
            //对广播状态进行处理
            @Override
            public void processBroadcastElement(String value,
                                                Context ctx,
                                                Collector<String> out) throws Exception {
                //获取广播状态
                BroadcastState<String, String> broadcastState = ctx.getBroadcastState(desc);
                //将数据传入到广播状态中
                broadcastState.put("switch",value);
            }
        })
                //结果打印输出
        .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
